package com.news.yp.baselibrary.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.news.yp.baselibrary.annotation.ViewById;

/**
 * 参数控制器
 */
public class EasyDialogController {

    private EasyDialog mDialog;
    private Window window;
    private EasyDialogHelper viewHelper;

    public EasyDialogController(EasyDialog dialog, Window window) {
        this.mDialog = dialog;
        this.window = window;
    }

    public void setViewHelper(EasyDialogHelper viewHelper) {
        this.viewHelper = viewHelper;
    }

    //设置文本
    public void setText(int viewId, CharSequence text) {
        viewHelper.setText(viewId, text);
    }

    public <T extends View> T getView(int vieId) {
        return viewHelper.getView(vieId);
    }

    public void setOnClickListener(int viewid, View.OnClickListener listener) {
        viewHelper.setOnclickListener(viewid, listener);
    }

    public EasyDialog getmDialog() {
        return mDialog;
    }

    public Window getWindow() {
        return window;
    }

    public static class EasyParams {
        public Context mContext;
        public int mthemeResId;
        //params里面的参数准备好
        public boolean mCancelable = true;//点击空白是否可以取消，默认是可以的
        public DialogInterface.OnCancelListener mOncancelListener;//点击取消的监听
        public DialogInterface.OnDismissListener onDismissListener;//dismiss的监听
        public DialogInterface.OnKeyListener onKeyListener;//key监听
        public View mView;                                //布局View
        public int mViewLayoutResId;  //布局layout的id
        public SparseArray<CharSequence> mTextArray = new SparseArray<>();//存放字体的修改
        // 存放点击事件
        public SparseArray<View.OnClickListener> mClickArray = new SparseArray<>();
        public int mWidth = ViewGroup.LayoutParams.WRAP_CONTENT;  //宽度，默认是包裹
        public int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mAnimations = 0; //动画
        public int mGravity = Gravity.CENTER;


        public EasyParams(Context context, int themeResId) {
            this.mContext = context;
            this.mthemeResId = themeResId;
        }

        public void apply(EasyDialogController dialogController) {

            EasyDialogHelper viewHelper = null;
            if (mViewLayoutResId != 0) {
                viewHelper = new EasyDialogHelper(mContext, mViewLayoutResId);
            }
            if (mView != null) {
                viewHelper = new EasyDialogHelper();
                viewHelper.setContentView(mView);
            }
            if (viewHelper == null) {
                throw new IllegalArgumentException("请先把参数布局整好，我们在继续哈");
            }
            dialogController.getmDialog().setContentView(viewHelper.getmContentView());

            //创建controller的辅助类
            dialogController.setViewHelper(viewHelper);
            //设置文本
            int textArraySize = mTextArray.size();
            for (int i = 0; i < textArraySize; i++) {
                dialogController.setText(mTextArray.keyAt(i), mTextArray.valueAt(i));
            }
            //设置点击事件
            int clickArraySize = mClickArray.size();
            for (int i = 0; i < clickArraySize; i++) {
                dialogController.setOnClickListener(mClickArray.keyAt(i), mClickArray.valueAt(i));
            }
            //配置自定义效果
            Window window = dialogController.getWindow();
            //设置定位
            window.setGravity(mGravity);

            // 设置动画
            if (mAnimations != 0) {
                window.setWindowAnimations(mAnimations);
            }

            // 设置宽高
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);
        }
    }
}
